<template>
  <div style="padding: 20px;height: 670px">
    <t-card style="height: 670px">
      <t-card shadow="never" style="width: 1320px">
        <el-form :model="userQO" ref="userQO" :inline="true">
          <el-form-item label="用户账号" prop="userName" label-width="300px">
            <el-input v-model="userQO.userName" filterable clearable placeholder="请输入用户账号"
                      style="width:160px;"></el-input>
          </el-form-item>
          <el-form-item label="用户状态" prop="userStatus" label-width="150px">
            <el-select v-model="userQO.userStatus" filterable clearable placeholder="请选择状态"
                       style="width: 120px;">
              <el-option v-for="s in status" :key="s.value" :label="s.label" :value="s.value">
              </el-option>
            </el-select>
          </el-form-item>
          &nbsp;&nbsp;
          <t-button theme="primary" variant="outline" size="large" @click="select(1)">查询</t-button>&nbsp;&nbsp;&nbsp;&nbsp;
          <t-button theme="primary" size="large"
                    @click="resetForm('userQO')">重置
          </t-button>&nbsp;&nbsp;&nbsp;&nbsp;
        </el-form>
      </t-card>
      <br/>

      <t-card shadow="never" style="width: 1320px">
        <el-table :data="currentUsers" border style="height:425px;">
          <el-table-column type="index" align="center"></el-table-column>
          <el-table-column prop="userName" label="用户账号" align="center"></el-table-column>
          <el-table-column prop="userPassword" label="用户密码" align="center"></el-table-column>
          <el-table-column prop="userStatus" label="用户状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.userStatus == 0">停用</span>
              <span v-if="scope.row.userStatus == 1">启用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-row>
                <t-button theme="primary" circle plain @click="edit(scope)">编辑</t-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <br/>
        <el-pagination background layout="prev, pager, next" :current-page.sync="currentPage"
                       :page-count="totalPage" @current-change="goPage">
        </el-pagination>
      </t-card>

      <el-dialog title="修改密码" :visible.sync="dialogUpdate">
        <el-form :model="updateUser" ref="updateUser">
          <el-row>
            <el-col :span="10" :offset="0">
              <el-form-item label="密码" prop="userPassword" label-width="80px">
                <el-input v-model="updateUser.userPassword" filterable clearable
                          placeholder="请输入密码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10" :offset="0">
              <el-form-item label="状态" prop="userStatus" label-width="80px">
                <el-select v-model="updateUser.userStatus" filterable clearable placeholder="请选择状态">
                  <el-option v-for="s in status" :key="s.value" :label="s.label" :value="s.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <t-button theme="primary" variant="outline" @click="dialogUpdate=false">取 消</t-button>
          &nbsp;
          <t-button theme="primary" @click="update">确 定</t-button>
        </div>
      </el-dialog>
    </t-card>
  </div>
</template>

<script>
import axios from "axios"
import {MessagePlugin} from "tdesign-vue";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Accounts",
  data() {
    return {
      userQO: {},
      allUsers: [],
      currentUsers: [],

      dialogUpdate: false,
      updateUser: {},
      status: [{
        value: '0',
        label: '停用'
      }, {
        value: '1',
        label: '启用'
      }],
      totalPage: 0,
      currentPage: 0,
    }
  },
  methods: {
    edit(scope) {
      this.dialogUpdate = true;
      //数据回显
      this.updateUser = {
        ...scope.row
      }
    },
    update() {
      if (this.updateUser.userId) {
        axios.post("api/admin/user/update", this.updateUser)
            .then(res => {
              if (res.data.code == 200) {
                this.dialogUpdate = false;
                this.goPage(this.currentPage);
                MessagePlugin.success("更新成功")
              }
            })
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    select(p) {
      axios.get("api/admin/user/list/" + p, {
        params: this.userQO
      }).then(res => {
        if (res.data.code == 200) {
          this.currentUsers = res.data.data.list;
          this.totalPage = res.data.data.pages;
          this.currentPage = 1;
          MessagePlugin.success("查询成功")
        }
      })
    },
    goPage(p) {
      axios.get("api/admin/user/list/" + p, {
        params: this.userQO
      }).then(res => {
        if (res.data.code == 200) {
          this.currentUsers = res.data.data.list;
          this.totalPage = res.data.data.pages;
        }
      })
    },
    getAll() {
      axios.get("api/admin/user/getAll").then(res => {
        if (res.data.code == 200) {
          this.allUsers = res.data.data;
        }
      })
    },
  },
  mounted() {
    this.getAll();
    this.goPage(1);
  }
}
</script>

<style>

</style>